package org.aristotle.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.demo.model.entity.Admin;
import org.aristotle.demo.mapper.AdminMapper;
import org.aristotle.demo.service.IAdminService;
import org.aristotle.demo.model.vo.AdminVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Override
    public Page<AdminVO> queryPageList(Long id, String adminName, Integer current, Integer size) {

        QueryWrapper<Admin> queryWrapper = new QueryWrapper();

        // 判断id是否为空
        if (id != null) {
            queryWrapper.eq("id",id);
        }

        // 判断adminName是否为空
        if (!StringUtils.isBlank(adminName)) {
            queryWrapper.like("admin_name",adminName);
        }

        Page<Admin> page = this.page(new Page<Admin>(current,size), queryWrapper);

        // 转为VO
        Page<AdminVO> result = AristotleBeanUtils.transitionBean(page, Page<AdminVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),AdminVO::new));
        return result;
    }

    @Override
    public AdminVO getById(Long id) {
        Admin book = this.baseMapper.selectById(id);

        AdminVO adminVO = AristotleBeanUtils.transitionBean(book, AdminVO::new);

        return adminVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {

        return super.removeById(id);
    }


}
